<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
  <title>Prototype Unit test file</title>
  <meta http-equiv="content-type" content="text/html; charset=utf-8" />
  <script src="../../dist/prototype.js" type="text/javascript"></script>
  <script src="../lib/unittest.js" type="text/javascript"></script>
  <link rel="stylesheet" href="../test.css" type="text/css" />
  <style type="text/css" media="screen">
  /* <![CDATA[ */
    #testcss1 { font-size:11px; color: #f00; }
    #testcss2 { font-size:12px; color: #0f0; display: none; }
  /* ]]> */
  </style>
</head>
<body>
<h1>Prototype Unit test file</h1>
<p>
  Test of utility functions in string.js
</p>

<!-- Log output -->
<div id="testlog"> </div>

<!-- Tests follow -->
<script type="text/javascript" language="javascript" charset="utf-8">
// <![CDATA[
  var evalScriptsCounter = 0;

  new Test.Unit.Runner({
    testGsubWithReplacementFunction: function() {with(this) {
      var source = 'foo boo boz';

      assertEqual('Foo Boo BoZ',
        source.gsub(/[^o]+/, function(match) {
          return match[0].toUpperCase()
        }));
      assertEqual('f2 b2 b1z',
        source.gsub(/o+/, function(match) {
          return match[0].length;
        }));
    }},
    
    testGsubWithReplacementString: function() {with(this) {
      var source = 'foo boo boz';

      assertEqual('foobooboz',
        source.gsub(/\s+/, ''));
      assertEqual('  z', 
        source.gsub(/(.)(o+)/, ''));
    }},
    
    testGsubWithReplacementTemplateString: function() {with(this) {
      var source = 'foo boo boz';

      assertEqual('-oo-#{1}- -oo-#{1}- -o-#{1}-z',
        source.gsub(/(.)(o+)/, '-#{2}-\\#{1}-'));
      assertEqual('-foo-f- -boo-b- -bo-b-z',
        source.gsub(/(.)(o+)/, '-#{0}-#{1}-'));
      assertEqual('-oo-f- -oo-b- -o-b-z',
        source.gsub(/(.)(o+)/, '-#{2}-#{1}-'));
      assertEqual('  z',
        source.gsub(/(.)(o+)/, '#{3}'));      
    }},
    
    testSubWithReplacementFunction: function() {with(this) {
      var source = 'foo boo boz';

      assertEqual('Foo boo boz',
        source.sub(/[^o]+/, function(match) {
          return match[0].toUpperCase()
        }), 1);
      assertEqual('Foo Boo boz',
        source.sub(/[^o]+/, function(match) {
          return match[0].toUpperCase()
        }, 2), 2);
      assertEqual(source,
        source.sub(/[^o]+/, function(match) {
          return match[0].toUpperCase()
        }, 0), 0);
      assertEqual(source,
        source.sub(/[^o]+/, function(match) {
          return match[0].toUpperCase()
        }, -1), -1);
    }},
    
    testSubWithReplacementString: function() {with(this) {
      var source = 'foo boo boz';

      assertEqual('oo boo boz',
        source.sub(/[^o]+/, ''));
      assertEqual('oooo boz',
        source.sub(/[^o]+/, '', 2));
      assertEqual('-f-oo boo boz',
        source.sub(/[^o]+/, '-#{0}-'));
      assertEqual('-f-oo- b-oo boz',
        source.sub(/[^o]+/, '-#{0}-', 2));
    }},
    
    testScan: function() {with(this) {
      var source = 'foo boo boz', results = [];
      source.scan(/[o]+/, function(match) {
        results.push(match[0].length);
      });
      assertEnumEqual([2, 2, 1], results);
      assertEqual(source, source.scan(/x/, fail));
    }},
    
    testToArray: function() {with(this) {
      assertEnumEqual([],''.toArray());
      assertEnumEqual(['a'],'a'.toArray());
      assertEnumEqual(['a','b'],'ab'.toArray());
      assertEnumEqual(['f','o','o'],'foo'.toArray());
    }},
    
    testCamelize: function() {with(this) {
      assertEqual('foo', 'foo'.camelize());
      assertEqual('foo_bar', 'foo_bar'.camelize());
      assertEqual('FooBar', 'FooBar'.camelize());

      assertEqual('fooBar', 'foo-bar'.camelize());
      assertEqual('borderBottomWidth', 'border-bottom-width'.camelize());
      
      assertEqual('classNameTest','class-name-test'.camelize());
      assertEqual('classNameTest','className-test'.camelize());
      assertEqual('classNameTest','class-nameTest'.camelize());
    }},
    
    testTruncate: function() {with(this) {
      var source = 'foo boo boz foo boo boz foo boo boz foo boo boz';
      assertEqual(source, source.truncate(source.length));
      assertEqual('foo boo boz foo boo boz foo...', source.truncate(0));
      assertEqual('fo...', source.truncate(5));
      assertEqual('foo b', source.truncate(5, ''));
    }},
    
    testStrip: function() {with(this) {
      assertEqual('hello world', '   hello world  '.strip());
      assertEqual('hello world', 'hello world'.strip());
      assertEqual('hello  \n  world', '  hello  \n  world  '.strip());
      assertEqual('', ' '.strip());
    }},
    
    testStripTags: function() {with(this) {
      assertEqual('hello world', 'hello world'.stripTags());
      assertEqual('hello world', 'hello <span>world</span>'.stripTags());
      assertEqual('hello world', '<a href="#" onclick="moo!">hello</a> world'.stripTags());
      assertEqual('hello world', 'h<b><em>e</em></b>l<i>l</i>o w<span class="moo" id="x"><b>o</b></span>rld'.stripTags());
    }},
    
    testStripScripts: function() {with(this) {
      assertEqual('foo bar', 'foo bar'.stripScripts());
      assertEqual('foo bar', ('foo <script>boo();<'+'/script>bar').stripScripts());
      assertEqual('foo bar', ('foo <script type="text/javascript">boo();\nmoo();<'+'/script>bar').stripScripts());
    }},
    
    testExtractScripts: function() {with(this) {
      assertEnumEqual([], 'foo bar'.extractScripts());
      assertEnumEqual(['boo();'], ('foo <script>boo();<'+'/script>bar').extractScripts());
      assertEnumEqual(['boo();','boo();\nmoo();'], 
        ('foo <script>boo();<'+'/script><script type="text/javascript">boo();\nmoo();<'+'/script>bar').extractScripts());
      assertEnumEqual(['boo();','boo();\nmoo();'], 
        ('foo <script>boo();<'+'/script>blub\nblub<script type="text/javascript">boo();\nmoo();<'+'/script>bar').extractScripts());
    }},
    
    testEvalScripts: function() {with(this) {
      assertEqual(0, evalScriptsCounter);
  
      ('foo <script>evalScriptsCounter++<'+'/script>bar').evalScripts();
      assertEqual(1, evalScriptsCounter);
      
      var stringWithScripts = '';
      (3).times(function(){ stringWithScripts += 'foo <script>evalScriptsCounter++<'+'/script>bar' });
      stringWithScripts.evalScripts();
      assertEqual(4, evalScriptsCounter);
    }},
    
    testEscapeHTML: function() {with(this) {
      assertEqual('foo bar', 'foo bar'.escapeHTML());
      assertEqual('foo &lt;span&gt;bar&lt;/span&gt;', 'foo <span>bar</span>'.escapeHTML());
      assertEqual('foo ß bar', 'foo ß bar'.escapeHTML());
      
      assertEqual('a&lt;a href="blah"&gt;blub&lt;/a&gt;b&lt;span&gt;&lt;div&gt;&lt;/div&gt;&lt;/span&gt;cdef&lt;strong&gt;!!!!&lt;/strong&gt;g',
                  'a<a href="blah">blub</a>b<span><div></div></span>cdef<strong>!!!!</strong>g'.escapeHTML());
    }},
    
    testUnescapeHTML: function() {with(this) {
      assertEqual('foo bar', 'foo bar'.unescapeHTML());
      assertEqual('foo <span>bar</span>', 'foo &lt;span&gt;bar&lt;/span&gt;'.unescapeHTML());
      assertEqual('foo ß bar', 'foo ß bar'.unescapeHTML());
      
      assertEqual('a<a href="blah">blub</a>b<span><div></div></span>cdef<strong>!!!!</strong>g',
        'a&lt;a href="blah"&gt;blub&lt;/a&gt;b&lt;span&gt;&lt;div&gt;&lt;/div&gt;&lt;/span&gt;cdef&lt;strong&gt;!!!!&lt;/strong&gt;g'.unescapeHTML());
    }},
    
    testTemplateEvaluation: function() {with(this) {
      var source = '<tr><td>#{name}</td><td>#{age}</td></tr>';
      var person = {name: 'Sam', age: 21};
      var template = new Template(source);
      
      assertEqual('<tr><td>Sam</td><td>21</td></tr>',
        template.evaluate(person));
      assertEqual('<tr><td></td><td></td></tr>',
        template.evaluate({}));
    }},
    
    testToQueryParams: function() {with(this) {
      assertEnumEqual([], Object.keys(''.toQueryParams()));
      assertEnumEqual([], Object.keys('foo?'.toQueryParams()));
      assertEnumEqual(['a', 'b'], Object.keys('foo?a&b'.toQueryParams()));
      
      var result = 'a'.toQueryParams();
      assertEqual(undefined, result['a']);
      assert(result.hasOwnProperty('a'));
      
      result = 'a&b=c'.toQueryParams();
      assertEqual(undefined, result['a']);
      assert(result.hasOwnProperty('a'));
      assertEqual('c', result['b']);
      
      result = 'a%20b=c&d=e%20f&g=h'.toQueryParams();
      assertEqual('c', result['a b']);
      assertEqual('e f', result['d']);
      assertEqual('h', result['g']);
    }},
    
    testInspect: function() {with(this) {
      assertEqual('\'\'', ''.inspect());
      assertEqual('\'test\'', 'test'.inspect());
      assertEqual('\'test \\\'test\\\' "test"\'', 'test \'test\' "test"'.inspect());
    }}
  }, 'testlog');
// ]]>
</script>
</body>
</html>
